<!--修改密码-->
<template>
    <el-dialog title="账号安全" :visible="dialogFormVisible" width="50%" @close="modecClose">
        <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2"  class="demo-ruleForm">
            <el-form-item  prop="oldPass">
                <el-input type="password" v-model="ruleForm2.oldPass" auto-complete="off" placeholder="原密码"></el-input>
            </el-form-item>
            <el-form-item  prop="newPass">
                <el-input type="password" v-model="ruleForm2.newPass" auto-complete="off" placeholder="设置密码"></el-input>
            </el-form-item>
            <el-form-item prop="checkPass">
                <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off" placeholder="确认密码"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="updatePassWord('ruleForm2')" style="width: 100%;">确认</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

</template>

<script>
import http from "../../components/axios/fetch";
import { resetPassword } from "../../components/axios/url";
import {UserHelper} from "../../components/helpers/UserHelper";
    export default {
        name: "ChangePwd",
        props:{
            dialogFormVisible: null
        },
        data(){
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('原密码不能为空'));
                }
                else {
                    callback();
                }
            };
            var validatePass1 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('新密码不能为空'));
                } else {
                    if (this.ruleForm2.checkPass !== '') {
                        this.$refs.ruleForm2.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入确认密码'));
                } else if (value !== this.ruleForm2.newPass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm2: {
                    oldPass: '',
                    newPass: '',
                    checkPass: ''

                },
                rules2: {
                    oldPass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    newPass: [
                        { validator: validatePass1, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ]

                }
            };
        },
        methods:{
            updatePassWord(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        const  data = {
                            oldPassword: this.ruleForm2.oldPass,
                            newPassword: this.ruleForm2.newPass
                        }
                        try{
                            http.post(resetPassword,{...data}).then(res => {
                                if(res.data && res.data.resultMess === "success") {
                                    UserHelper.clearToken()
                                    UserHelper.clearUserId();
                                    UserHelper.clearUserName();
                                    UserHelper.clearUserCostCenterId();
                                    UserHelper.clearUserCostCenter();
                                    this.modecClose(); // 关闭弹窗
                                    this.$router.push({ path: '/login' })
                                    this.$message({
                                        message: '密码修改成功, 请用新密码登录',
                                        type: 'success'
                                    });

                                }
                            })
                        } catch(error){
                            this.$notify.error({
                                title: '提示',
                                message: '网络错误',
                            });
                        }
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            modecClose() {
                this.$emit('dialogFormVisibleClose');
            }
        }
    }
</script>

<style scoped>

</style>
